<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class xk extends MY_Controller {
    function __construct()
    {
        // Call the Model constructor
        parent::__construct();
        $this->lang->load('vt');
        $this->load->library('form_validation');
        $this->load->helper('form');
        //$this->load->helper('date');
    }
    /*
	public function index()
    {
        $this->load->model('vt_model');
        
        $this->template->set_view('nk/content_home');
        
        $this->template->render();        
        //$this->load->view('welcome_message');
    }*/
    
    public function export(){
        //$this->template->set_regions('header','<h3>Hearder ne con</h3>');
        //$this->template->set_regions('footer','<h3>Footer ne con</h3>');
        //echo "ssssss";
        //$this->template->set('bien','1002');
        if( !has_permission('KHVT.Product.Export') ){
            redirect('no_access');
        }
        
        $this->load->model('vt_model');
        $products = $this->vt_model->getALLProduct();
        
        $this->template->set('products',$products);
        
        $construction = $this->vt_model->getConstructionByStatus(1);
        $this->template->set('construction',$construction);
        
        $categories = $this->vt_model->getAllCategory();
        $this->template->set('categories',$categories);
        
        $employee = $this->vt_model->getEmployee_recipient();
        $this->template->set('employee',$employee);
        
        $date = date('d-m-Y');
        $date_code = str_replace('-', '', $date);
        $count_bill = $this->vt_model->countExportBillByDate(dateVnToMysql($date));
        $count_bill += 1;
        $this->template->set('bill_code',$date_code.$count_bill);
        
        $this->template->set_page_title( lang('vt_phieuxuatkho') );
        $this->template->set_view('xk/content');
        
        $this->template->render();
        
        
		//$this->load->view('welcome_message');
	}
    
    public function ajax_loadfrm_export(){
        $this->load->model('vt_model');
        if ($_SERVER['REQUEST_METHOD'] == 'POST'){
            $id = $this->input->post('id');
            $import_total = $this->input->post('import_total');
            $import_price = $this->input->post('import_price');
            $note = $this->input->post('note');
            
            $data = array();

            $data['product'] = $this->vt_model->getVTById($id);

            if($import_total){
                $data['import_total'] = $import_total;
            }
            if($import_price){
                $data['import_price'] = $import_price;
            }
            if($note){
                $data['note'] = $note;
            }
            
            $this->load->view($this->template->template.'/xk/form_export_vt',$data);
        }
    }
    
    public function export_product(){
        $this->load->model('vt_model');
        if( !has_permission('KHVT.Product.Export') ){
            echo '';
            exit;
        }
        if ($_SERVER['REQUEST_METHOD'] == 'POST'){
            $cons_id = $this->input->post('cons_id');
            $bill_code = $this->input->post('bill_code');
            $created_by = $this->input->post('created_by');
            $recipient_id = $this->input->post('recipient');
            $bill_note = $this->input->post('bill_note');
            $product_total = $this->input->post('product_total');
            $product_price = $this->input->post('product_price');
            $product_note = $this->input->post('product_note');
            $bill_total = $this->input->post('bill_total');
            $bill_vat = $this->input->post('bill_vat');
            $bill_vatprice = $this->input->post('bill_vatprice');
            $bill_totalvat = $this->input->post('bill_totalvat');
            
            //$this->form_validation->set_rules('created_by', 'created by', 'required||numeric');
            //$this->form_validation->set_rules('bill_code', 'bill code', 'required||callback_uniquecode_check');
            //$this->form_validation->set_rules('recipient', 'recipient', 'required||numeric');
            //$this->form_validation->set_rules('bill_note', 'note', 'required');
            //$this->form_validation->set_rules('product_total', 'product total', 'required||callback_arraynumeric_check');
            //$this->form_validation->set_rules('product_note', 'product note', 'required');

            $flag = true;
            $error = array();
            $url_redirect = '';
            
            if( $bill_code == '' ){
                $flag = false;
                $error[] = array(
                    'message' => lang('vt_error_035'),
                    'name' => 'bill_code'
                );
            }else{
                if( !$this->vt_model->unique_export_bill_code($bill_code) ){
                    $flag = false;
                    $error[] = array(
                        'message' => lang('vt_error_036'),
                        'name' => 'bill_code'
                    );
                }
            }
            
            if($bill_vat == ''){
                $flag = false;
                $error[] = array(
                    'message' => lang('vt_error_037'),
                    'name' => 'bill_vat'
                );
            }else{
                if(!numeric_check($bill_vat)){
                    $flag = false;
                    $error[] = array(
                        'message' => lang('vt_error_038'),
                        'name' => 'bill_vat'
                    );
                }else{
                    if($bill_vat < 0){
                        $flag = false;
                        $error[] = array(
                            'message' => lang('vt_error_039'),
                            'name' => 'bill_vat'
                        );
                    }
                }
            }
            
            if($created_by == '' || $created_by == 0){
                $flag = false;
                $error[] = array(
                    'message' => lang('vt_error_040'),
                    'name' => 'created_by'
                );
            }
            
            if($cons_id == false){
                $flag = false;
                $error[] = array(
                    'message' => lang('vt_error_041'),
                    'name' => 'supplier_id'
                );
            }
            
            if($recipient_id == false){
                $flag = false;
                $error[] = array(
                    'message' => lang('vt_error_042'),
                    'name' => 'recipient'
                );
            }
            
            if($product_total == false){
                $flag = false;
                $error[] = array(
                    'message' => lang('vt_error_043'),
                    'name' => 'table_product'
                );
            }else{
                if(!$this->vt_model->arraynumeric_check($product_total)){
                    $flag = false;
                    $error[] = array(
                        'message' => lang('vt_error_043'),
                        'name' => 'table_product'
                    );
                }
            }
            
            if($flag == true){
                $count_bill_total = 0;
                $count_bill_vat_price = 0;
                $count_bill_total_vat = 0;
                //insert import_bill
                $import_data = array(
                    'cons_id' => $cons_id,
                    'bill_code' => $bill_code,
                    'created_date' => date("Y-m-d H:i:s"),
                    'created_by' => $created_by,
                    'recipient' => $recipient_id,
                    'bill_note' => $bill_note,
                    'bill_total' => $bill_total,
                    'bill_vat' => $bill_vat,
                    'bill_vatprice' => $bill_vatprice,
                    'bill_totalvat' => $bill_totalvat,
                    'revert' => 0,
                );
                
                $bill_id = $this->vt_model->insert_export_bill($import_data);
                
                //insert product
                foreach($product_total As $pro_id => $total){
                    $product = $this->vt_model->getVTById($pro_id);
                    $last_total = $product->total-$total;
                    $this->vt_model->updateVT(array('total'=>$last_total),$product->id);
                    
                    $count_bill_total += $total*$product->price;
                    $pro_data = array(
                        'bill_id' => $bill_id,
                        'product_id' => $pro_id,
                        'export_total' => $total,
                        'export_price' => $product->price,
                        'first_total' => $product->total,
                        'last_total' => $last_total,
                        'revert_total' => 0,
                        'note' => ( isset($product_note[$pro_id]) ) ? $product_note[$pro_id] : '',
                    );
                    
                    $product_id = $this->vt_model->insert_export_product($pro_data);
                }
                $count_bill_vat_price = $count_bill_total * $bill_vat / 100;
                $count_bill_total_vat = $count_bill_total + $count_bill_vat_price;
                $this->vt_model->updateExportBill(array(
                    'bill_total' => $count_bill_total,
                    'bill_vatprice' => $count_bill_vat_price,
                    'bill_totalvat' => $count_bill_total_vat
                ),$bill_id);
                
                $khvtReport = new khvt_report();
                $khvtReport->update_report(date("Y-m-d"));
                $url_redirect = site_url('xk/bill/'.$bill_id);
                //redirect('xk/bill/'.$bill_id);
            }
            
            $data = array(
                'flag' => $flag,
                'error' => $error,
                'url' => $url_redirect
            );
            echo json_encode($data);
        }
    }
    
    public function arraynumeric_check($arr_nummber){
        if( count($arr_nummber) > 0 ){
            foreach($arr_nummber As $number){
                $isnumber = (bool)preg_match( '/^[\-+]?[0-9]*\.?[0-9]+$/', $number);
                if( !$isnumber ){
                    $this->form_validation->set_message('arraynumeric_check', 'ban phai nhap chu so, ko phai la text');
                    return false;
                }
            }
        }else{
            return false;
        }
        return true;
    }
    
    public function uniquecode_check($code){
        if($code == ''){
            return false;
        }else{
            $this->load->model('vt_model');
            if( !$this->vt_model->unique_export_bill_code($code) ){
                $this->form_validation->set_message('uniquecode_check', 'Bill code nay da dc su dung');
                return false;
            }
        }
        return true;
    }
    
    public function bill($id=null)
    {
        //$this->template->set_regions('header','<h3>Hearder ne con</h3>');
        //$this->template->set_regions('footer','<h3>Footer ne con</h3>');
        //echo "ssssss";
        //$this->template->set('bien','1002');
        $this->load->model('vt_model');
        $bill = $this->vt_model->getExportBillById($id);
        
        $this->template->set('bill',$bill);
        $this->template->set_page_title( lang('vt_title_exportbilldetail') );
        $this->template->set_view('xk/content_bill_view');
        
        $this->template->render();
        
        
        //$this->load->view('welcome_message');
    }
    
    public function datatable_bill($bill_id=0){
        $this->load->model('vt_model');
        $aaData = $this->vt_model->load_datatable_export_product($_GET,1,$bill_id);
        $totalData = $this->vt_model->load_datatable_export_product($_GET,2,$bill_id);
        
        $output = array(
            "sEcho" => intval($_GET['sEcho']),
            "iTotalRecords" => $aaData['total'],
            "iTotalDisplayRecords" => $totalData['total'],//$iFilteredTotal,
            "aaData" => $aaData['aaData']
        );
    
        echo json_encode( $output );
    }
    
    public function datatable_filter_bill_import(){
        $this->load->model('vt_model');
        $aaData = $this->vt_model->load_datatable_filter_bill_import($_GET,1);
        $totalData = $this->vt_model->load_datatable_filter_bill_import($_GET,2);
        
        $output = array(
            "sEcho" => intval($_GET['sEcho']),
            "iTotalRecords" => $aaData['total'],
            "iTotalDisplayRecords" => $totalData['total'],//$iFilteredTotal,
            "aaData" => $aaData['aaData']
        );
    
        echo json_encode( $output );
    }
    
    public function ajax_loadfrm_revert(){
        $this->load->model('vt_model');
        if ($_SERVER['REQUEST_METHOD'] == 'POST'){
            $id = $this->input->post('id');
            $data = array();
            $data['product'] = $this->vt_model->getExportProductById($id);
            $this->load->view($this->template->template.'/xk/form_revert_vt',$data);
        }
    }
    
    public function revert_product(){
        $this->load->model('vt_model');
        if ($_SERVER['REQUEST_METHOD'] == 'POST'){
            $id = $this->input->post('id');
            $revert = $this->input->post('revert');
            $flag = true;
            $error = array();
            
            $export_product = $this->vt_model->getExportProductById($id);
            if($export_product){
                if($revert == ''){
                    $flag = false;
                    $error[] = array(
                        'message' => lang('vt_error_045'),
                        'name' => 'revert'
                    );
                }elseif($revert == 0){
                    $flag = false;
                    $error[] = array(
                        'message' => lang('vt_error_046'),
                        'name' => 'revert'
                    );
                }else{
                    $istotal = (($export_product->revert_total + $revert) > $export_product->export_total)?false:true;
                    if(!$istotal){
                        $flag = false;
                        $error[] = array(
                            'message' => lang('vt_error_047'),
                            'name' => 'revert'
                        );
                    }
                }
            }else{
                $flag = false;
                $error[] = array(
                    'message' => lang('vt_error_048'),
                    'name' => 'revert'
                );
            }
            if($flag == true){
                $product = $this->vt_model->getVTById($export_product->product_id);
                $last_total = $product->total + $revert;
                $this->vt_model->updateExportBill(array('revert'=>1),$export_product->bill_id);
                $this->vt_model->updateVT(array('total'=>$last_total),$product->id);
                $this->vt_model->updateExportProduct(
                    array('revert_total'=>$export_product->revert_total+$revert),
                    $id
                );
                $revert_product = $this->vt_model->insert_revert_product(array(
                    'bill_id' => $export_product->bill_id,
                    'product_id' => $export_product->product_id,
                    'revert_total' => $revert,
                    'first_total' => $product->total,
                    'last_total' => $last_total,
                    'revert_date' => date("Y-m-d H:i:s")
                ));
                $khvtReport = new khvt_report();
                $khvtReport->update_report(date("Y-m-d"));
            }
            $data = array(
                'flag' => $flag,
                'error' => $error,
                'id' => $id
            );
            print json_encode($data);
        }
    }
    
    public function datatable_revert($bill_id=0){
        $this->load->model('vt_model');
        $aaData = $this->vt_model->load_datatable_revert_product($_GET,1,$bill_id);
        $totalData = $this->vt_model->load_datatable_revert_product($_GET,2,$bill_id);
        
        $output = array(
            "sEcho" => intval($_GET['sEcho']),
            "iTotalRecords" => $aaData['total'],
            "iTotalDisplayRecords" => $totalData['total'],//$iFilteredTotal,
            "aaData" => $aaData['aaData']
        );
    
        echo json_encode( $output );
    }
}

/* End of file welcome.php */
/* Location: ./application/controllers/welcome.php */